let obj = {name: 'Dashu', age: 90}
let obj2 = {...obj}
let obj22 = {name: 'Hak', h: '123'}
console.log(obj === obj2) // false
console.log(obj == obj2) //false
// 拷贝， 合并
let school = {...obj, ...obj2, ...obj22}
console.log(school)

// 这种拷贝是浅拷贝
let obj3 = {name: {sName: 'Dashu'}}
let newObj3 = {...obj3}
console.log(obj3 == newObj3) // false
console.log(obj3.name == newObj3.name) // true
newObj3.name.sName = 'wj'
console.log(obj3.name.sName) // wj

// 如何实现一个深度拷贝  递归拷贝
let obj4 = {name: {sName: 'Dashu', obj: {age: 23}}, a: 1, reg: undefined}
// let obj4 = /\d+/
function deepClone(obj) {
  if(obj === null) return null
  if(typeof obj != 'object') return obj // 函数拷贝不需要管
  if(obj instanceof RegExp) return new RegExp(obj)
  if(obj instanceof Date) return new Date(obj)
  let newObj = new obj.constructor
  for(let key in obj) {
    // 递归
    newObj[key] = deepClone(obj[key])
  }
  return newObj
}
let newReg = deepClone(obj4)
newReg.name.obj.age = 80
console.log(newReg)
console.log(obj4)
console.log(newReg ===obj4)

// Object.assign es6
let k1 = {age: 12}
let k2 = {name: 'Dashu'}
let o = {}
o = Object.assign({}, k1, k2)
// let k3 = {}
// Object.assign(k3, k1, k2)
// console.log(k3)
console.log(o)
console.log(k1)

// 数组的展开 es6
let arr1 = [1, 2]
let arr2 = [3, 4, 5]
console.log([...arr1, ...arr2])

// 剩余运算符  只能放在函数的最后一个参数
function sum(...arg) {
  // return eval(arg.join('+'))
  console.log(arg)
  console.log(typeof arguments)
  console.log(arguments[0])
  return arg.join('+')
}
let r = sum(1,2,3,4,5)
console.log(r)